﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Managistics.Utility.Common.View.Components.Test
{
    public class NNTextBox<T> : TextBox
    {
        private IList<T> dataSource;
        private string nameHolding;
        private string nameDisplay;
        private T selected;

        public NNTextBox(IList<T> dataSource, string nameHolding, string nameDisplay)
        {
            this.dataSource = dataSource;
            this.nameHolding = nameHolding;
            this.nameDisplay = nameDisplay;

            this.initAutoComplete();
            this.HideSelectionChanged += this.CustHideSelectionChanged;

        }

        public IList<T> DataSource
        {
            get { return this.dataSource; }
            set { this.dataSource = value; }
        }

        public string NameHolding
        {
            get { return this.nameHolding; }
            set { this.nameHolding = value; }
        }

        public string NameDisplay
        {
            get { return this.nameDisplay; }
            set { this.nameDisplay = value; }
        }

        public T ItemSelected
        {
            get { return this.selected; }
            set { this.selected = value; }
        }

        private void initAutoComplete()
        {
            this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            if (this.DataSource != null && this.DataSource.Count > 0)
            {
                List<string> list = new List<string>();
                foreach (T obj in this.DataSource)
                {
                    list.Add(obj.GetType().GetProperty(this.NameDisplay).GetValue(obj, null).ToString());
                }
                auto.AddRange(list.ToArray());
                this.AutoCompleteCustomSource = auto;
            } 
        }

        private void CustHideSelectionChanged(Object sender, EventArgs e)
        {
            this.Text = "hello";
        }
    }
}
